package com.pk.platform.business.charge.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.pk.platform.business.charge.dao.IIncomeExpenseIndexDao;
import com.pk.platform.business.core.dao.impl.GenericDaoImpl;
import com.pk.platform.domain.charge.IncomeExpenseIndex;
import com.pk.platform.util.StringConverter;
import com.pk.platform.util.page.ListPage;
import com.pk.platform.util.page.Pager;

public class IncomeExpenseIndexDaoImpl extends GenericDaoImpl implements
IIncomeExpenseIndexDao {

	public ListPage queryIncomeExpenseIndex(IncomeExpenseIndex iei, Pager pager) {
		StringBuffer sql = new StringBuffer();
		sql.append("select ci.id,ci.indexName,ci.isDel,bd.dataValue from income_expense_index ci left join p_basedata bd on ci.bdid=bd.id where ci.isDel=0 ");
		List<Object> param = new ArrayList<Object>();
		if(StringConverter.isNotNull(iei.getIndexName()))
		{
			sql.append(" and ci.indexName like ?");
			param.add("%" + iei.getIndexName() + "%");
		}
		if(iei.getType()!=null && StringConverter.isNotNull(iei.getType().getId()))
		{
			sql.append(" and bd.id= ?");
			param.add(iei.getType().getId());
		}
		return this.getListPage(sql.toString(), param.toArray(), pager);
	}
	
	public List<IncomeExpenseIndex> queryIncomeExpenseIndexList()
	{
		String hql = "from IncomeExpenseIndex ci where isDel = 0 order by ci.indexName";
		return this.getHibernateTemplate().find(hql);
	}
	
	
	public boolean existIncomeExpenseIndexForUpdate(String indexName, String id)
	{
		String sql = "select count(id) from income_expense_index where indexName = ? and id <> ?";
		int count = this.getSjt().queryForInt(sql, indexName, id);
		if(count > 0)
			return true;
		return false;
	}
	
	public boolean existIndexData(String indexId) {
		String sql = "select count(id) from income_expense where ieIndexId = ? ";
		int count = this.getSjt().queryForInt(sql, indexId);
		if(count > 0)
			return true;
		return false;
	}

}
